package com.mall.web.control.app.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.biz.user.UserBizImpl;
import com.mall.client.user.UserExtendedService;
import com.mall.pojo.UserExtended;
import com.mall.vo.user.UserVO;
import com.mall.web.annotation.CheckCommmonOnlyKey;
import com.mall.web.util.HttpUtil;
import com.mall.web.vo.ResponseEntity;

@Controller
@RequestMapping("/user/login/")
public class LoginController {
	
	private Logger log = LoggerFactory.getLogger(LoginController.class);
	
	@Autowired
	private UserBizImpl userBiz;

	@Autowired
	private UserExtendedService userExtendedService;
	

	@RequestMapping("login")
	public String login(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		String referer = HttpUtil.getHttpReferer(request);
		//modelMap.put("onlyKey", onlyKey);
		modelMap.put("referer", referer);
		return "user/login/login";
	}

	@CheckCommmonOnlyKey
	@RequestMapping("loginAction")
	@ResponseBody
	public ResponseEntity<UserVO> loginAction(HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {

		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		String deviceToken = request.getParameter("deviceToken");
		ResponseEntity<UserVO> responseEntity = new ResponseEntity<>();

		UserVO userVO = this.userBiz.login(request.getParameter("phone"), request.getParameter("passWord"), onlyKey);// 用户手机号登陆
		if (userVO == null) {
			responseEntity.initErrorResponseEntity(null, "账号或密码错误");
			return responseEntity;
		}
		UserExtended userExtended = this.userExtendedService.getUserExtendByUserId(userVO.getUser().getId());
		if(userExtended != null){
			String userDeviceToken = userExtended.getDeviceToken();
			if(StringUtils.isNotBlank(userDeviceToken) && StringUtils.isNotBlank(deviceToken)){
				if (!userDeviceToken.equals(deviceToken)) {// deviceToken不同时候更新
					userVO.setUserExtended(userExtended);
					/**
					 * 用户登陆更新app对应的deviceToken
					 */
					Boolean updateyoumengDeviceToken = this.userBiz.updateDeviceToken(userVO,
							request.getParameter("deviceToken"));
					log.info("更新deviceToken", updateyoumengDeviceToken);
				}
			}
		}
		
		

		Boolean sessResult = userBiz.updateOrInsertSess(request,response,userVO,onlyKey);
		log.info("更新session", sessResult);
		if(!sessResult){
			responseEntity.setCode("-300");
			responseEntity.setData(null);
			responseEntity.setMessage("sess insert or update error");
			return responseEntity;
		}
		responseEntity.initSuccResponseEntity(userVO, "login success");
		return responseEntity;
	}


}
